#!/bin/sh

. @KEDR_TRACE_TEST_COMMON_FILE@

tmpdir="@KEDR_TEST_PREFIX_TEMP_SESSION@/kedr_trace/simple_ordering"
mkdir -p ${tmpdir}

trace_file_copy="${tmpdir}/trace.txt"

if ! kedr_trace_test_load; then
	exit 1 # Error message is printed by the function itself.
fi

if ! @INSMOD@ @TRACE_TEST_TARGET_MODULE@; then
	printf "Failed to load target module for test.\n"
	kedr_trace_test_unload
	exit 1
fi

# Generate messages in the trace.
for i in 0 1 2 3 4; do
	echo "$i" > ${trace_generator_file}
done

if ! @RMMOD@ @TRACE_TEST_TARGET_MODULE_NAME@; then
	printf "Cannot unload target module for testing.\n"
	# Unloading test infrustructure will definitely fail
	exit 1
fi

# Use 'dd' for non-blocking read of trace file.
#
# This reading will be finished with EAGAIN error code, so
# 'dd' will return nonzero code.
dd if=${trace_file} of=${trace_file_copy} iflag=nonblock

if ! kedr_trace_test_unload; then
	exit 1 # Error message is printed by the function itself.
fi

# Verify trace
LC_ALL=C awk -f "../verify_trace_format.awk" "${trace_file_copy}"
if test $? -ne 0; then
	printf "Trace file has incorrect format.\n"
	exit 1
fi

LC_ALL=C awk -f "verify_trace.awk" "${trace_file_copy}"
if test $? -ne 0; then
	printf "Trace file is inconsistent.\n"
	exit 1
fi
